package top.kangyaocoding.tech.service.Impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.stereotype.Service;
import top.kangyaocoding.tech.common.utils.ChatRedisMemory;
import top.kangyaocoding.tech.service.IChatWithMemoryService;

/**
 * 描述:
 *
 * @author K·Herbert
 * @since 2025-04-16 11:57
 */
@Slf4j
@Service
public class ChatWithMemoryService implements IChatWithMemoryService {
    // 对话记忆长度
    private static final Integer CHAT_MEMORY_SIZE = 8;

    private final ChatClient chatClient;
    private final ChatRedisMemory chatMemory;

    public ChatWithMemoryService(ChatClient chatClient, ChatRedisMemory chatMemory) {
        this.chatClient = chatClient;
        this.chatMemory = chatMemory;
    }

    @Override
    public String chat(String userId, String userMessage) {
        MessageChatMemoryAdvisor messageChatMemoryAdvisor = new MessageChatMemoryAdvisor(chatMemory, userId, CHAT_MEMORY_SIZE);
        String text = chatClient
                .prompt()
                .options(ChatOptions.builder().model("deepseek-r1").build())
                .user(userMessage)
                .advisors(messageChatMemoryAdvisor)
                .call()
                .content();
        log.info("userId: {}, userMessage: {}, text: {}", userId, userMessage, text);
        return text;
    }
}

